package com.simple.service.fee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeReport;
import com.xunj.core.CoreDao;
import com.xunj.core.ParaUnit;
import com.xunj.service.system.IReportEngine;
import com.xunj.util.Common;
import com.xunj.util.Util;

public class FeeReimbursementReportService implements IReportEngine{
	
	private CoreDao dao;

	public FeeReimbursementReportService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 费用报销明细信息导出
	 * @return
	 * @throws Exception
	 */
	public Map exportFeeReimbursementDetail() throws Exception
	{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		FeeReport feeReport = new FeeReport();
		Map map=request.getParameterMap();
		Iterator its = map.entrySet().iterator();
		Util u = new Util();
		while (its.hasNext()) {
			Entry e = (Entry) its.next();
			String name = (String) e.getKey();
			if(name.indexOf("feeReport.")!=-1)
			{
				String[] values = (String[]) e.getValue();
				if(values[0] !=null && !values[0].equals(""))
					u.setPropertyValue(feeReport, name.substring(10, name.length()), values[0]);
			}
		}

		String hql = "select new Map(" +
				"feeReport.remitDate as remitDate," +
				"feeReport.areaName as areaName," +
				"feeReport.areaDeptName as areaDeptName," +
				"feeReport.remitState as remitState," +
				"feeReport.remitCompanyName as remitCompanyName," +
				"feeReport.remitRemark as remitRemark," +
				"feeDetail.feeTarget as feeTarget," +
				"feeDetail.targetName as targetName," +
				"feeWrittenOff.billAmountWrittenOff as billAmountWrittenOff," +
				"feeWrittenOff.billCountWrittenOff as billCountWrittenOff," +
				"feeWrittenOff.certificateAmountWrittenOff as certificateAmountWrittenOff," +
				"feeWrittenOff.certificateCountWrittenOff as certificateCountWrittenOff" +
				") from " +
				"FeeDetail feeDetail," +
				"FeeReport feeReport," +
				"FeeWrittenOff feeWrittenOff where " +
				"feeDetail.feeId = feeWrittenOff.feeId " +
				"and feeDetail.feeReportId = feeReport.feeReportId " ;
		hql+="and feeReport.reportState='已报销' ";

		if(feeReport.getRemitCompany()!=null&&!"".equals(feeReport.getRemitCompany()))
			hql+="and feeReport.remitCompany='"+feeReport.getRemitCompany()+"' ";
		

		String remitDateFrom = request.getParameter("remitDateFrom");
		String remitDateTo = request.getParameter("remitDateTo");
		if(!remitDateFrom.equals(""))
		{
			hql+="and feeReport.remitDate>='"+ remitDateFrom +" 00:00:00' ";
		}
		if(!remitDateTo.equals(""))
		{
			hql+="and feeReport.remitDate<='"+ remitDateTo +" 00:00:00' ";
		}
		if(feeReport.getAreaId()!=null&&!feeReport.getAreaId().equals(""))
			hql+="and feeReport.areaId='"+feeReport.getAreaId()+"' ";
		if(feeReport.getAreaDeptId()!=null&&!feeReport.getAreaDeptId().equals(""))
			hql+="and feeReport.areaDeptId='"+feeReport.getAreaDeptId()+"' ";
		
		if(feeReport.getFeeReportYear()!=null&&!feeReport.getFeeReportYear().equals(""))
			hql+="and feeReport.feeReportYear="+feeReport.getFeeReportYear()+" ";
		if(feeReport.getFeeReportMonth()!=null&&!feeReport.getFeeReportMonth().equals(""))
			hql+="and feeReport.feeReportMonth="+feeReport.getFeeReportMonth()+" ";
		if(feeReport.getFeeReportCode()!=null&&!feeReport.getFeeReportCode().equals(""))
			hql+="and feeReport.feeReportCode='"+feeReport.getFeeReportCode()+"' ";
		
		hql+=" order by feeReport.areaId,feeReport.areaDeptId";
		
		List datalist = dao.findAll(hql);
		Map beans = new HashMap();
		beans.put("datalist", datalist);

		return beans;
	}
	/**
	 * 票据，凭证汇总表
	 * @return
	 * @throws Exception
	 */
	public Map exportFeeReport() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		FeeReport feeReport = new FeeReport();
		Map map=request.getParameterMap();
		Iterator its = map.entrySet().iterator();
		Util u = new Util();
		while (its.hasNext()) {
			Entry e = (Entry) its.next();
			String name = (String) e.getKey();
			if(name.indexOf("feeReport.")!=-1)
			{
				String[] values = (String[]) e.getValue();
				if(values[0] !=null && !values[0].equals(""))
					u.setPropertyValue(feeReport, name.substring(10, name.length()), values[0]);
			}
		}
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		if(feeReport.getAreaId()!=null&&!feeReport.getAreaId().equals(""))
			paralist.add(new ParaUnit("areaId", feeReport.getAreaId(), ParaUnit.EQ));
		if(feeReport.getAreaDeptId()!=null&&!feeReport.getAreaDeptId().equals(""))
			paralist.add(new ParaUnit("areaDeptId", feeReport.getAreaDeptId(), ParaUnit.EQ));
		
		if(feeReport.getFeeReportYear()!=null&&!feeReport.getFeeReportYear().equals(""))
			paralist.add(new ParaUnit("feeReportYear", feeReport.getFeeReportYear(), ParaUnit.EQ));
		if(feeReport.getFeeReportMonth()!=null&&!feeReport.getFeeReportMonth().equals(""))
			paralist.add(new ParaUnit("feeReportMonth", feeReport.getFeeReportMonth(), ParaUnit.EQ));
		paralist.add(new ParaUnit("reportState", "已报销", ParaUnit.EQ));
		if(feeReport.getFeeReportCode()!=null&&!feeReport.getFeeReportCode().equals(""))
			paralist.add(new ParaUnit("feeReportCode", feeReport.getFeeReportCode(), ParaUnit.LIKE));
		if(feeReport.getRemitCompany()!=null&&!feeReport.getRemitCompany().equals(""))
			paralist.add(new ParaUnit("remitCompany", feeReport.getRemitCompany(), ParaUnit.EQ));
		
		String remitDateFrom = request.getParameter("remitDateFrom");
		String remitDateTo = request.getParameter("remitDateTo");
		if(!remitDateFrom.equals(""))
		{
			paralist.add(new ParaUnit("remitDate",Common.newDate(remitDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
			request.setAttribute("remitDateFrom", remitDateFrom);
		}
		if(!remitDateTo.equals(""))
		{
			paralist.add(new ParaUnit("remitDate",Common.newDate(remitDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
			request.setAttribute("remitDateTo", remitDateTo);
		}
		String filterData = (String) request.getAttribute("filterData");
		if(filterData.equals("bill"))
		{
			paralist.add(new ParaUnit("billAmountTol", 0d, ParaUnit.NE));
		}else
		{
			paralist.add(new ParaUnit("certificateAmountTol", 0d, ParaUnit.NE));
		}
		
		
		List list = dao.criteriaQuery(FeeReport.class, paralist);
		Map beans = new HashMap();
		beans.put("datalist", list);

		return beans;
	}
		
		
}
